package com.example.agron.user;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    /*
     * 自定义SQL语句
     * */
    @Query("SELECT s FROM User s WHERE s.email = ?1")
    Optional<User> findUserByEmail(String email);

    /*@Query("SELECT s FROM User s WHERE s.name LIKE %?1% AND (case when s.gender=?2 then s.gender=?2 else s.geder!=''  end)") //s.gender=?2*/
    @Query("SELECT s FROM User s WHERE s.name LIKE %?1% AND s.gender=?2")
    List<User> queryUserByNameLikeAndGender(String name, String gender);

    @Query("SELECT s FROM User s WHERE s.name LIKE %:name% or s.gender LIKE %:gender%")
    List<User> findUserByNameIsLikeOrGenderIsLike(String name, String gender);

    @Query("select s from User s where s.name like concat('%',?1,'%') and s.email=?2 and s.gender =?3  ")
    List<User> findUserByNameLikeAndEmailAndGender(String name, String email, String gender);

    /*
     *JAP形式(一定要findBy + 参数名， 模糊查询 findBy + 参数名 + Like)
     * */
    List<User> findUserByNameIs(String name);

    List<User> findUserByNameIsLikeAndGender(String name, String gender);

    List<User> findUserByNameAndGender(String name, String gender);

    List<User> findUserByGender(String gender);

    List<User> findUserByGenderContaining(String gender);

    List<User> findUserByGenderContains(String gender);

    Page<User> findUserByNameContainsAndEmailContainsAndGenderContains(String name, String email, String gender, Pageable pageable);

    void deleteAllById(Iterable<? extends Long> longs);

    /*@Modifying
    @Query(value = " update sys_user set avatar = ?1 where user_name = ?2 ", nativeQuery = true)
    Integer updateUserAvatar(String userName, String avatar);

    Integer deleteByUserIdIn(List<Long> userIds);*/

}
